<template>
	<view class="container">
		<view>
			<view class="form">
				<span>
					<view class="conwrap no-bg">
						<view class="cu-form-group">
							<input v-model="form.old_login_pwd" class="uni-input-input" type="text" password placeholder="Please enter the old password" />
						</view>
						<view class="cu-form-group"><input v-model="form.login_pwd" class="uni-input-input" type="text" password placeholder="Please set a new password" /></view>
						<view class="cu-form-group">
							<input v-model="form.c_login_pwd" class="uni-input-input" type="text" password placeholder="Please confirm new password" />
						</view>
					</view>
					<ZAlterLoginPwButton label="Confirm modification" @click="handleAlterLoginPw" />
				</span>
			</view>
		</view>
	</view>
</template>
<script>
import ZAlterLoginPwButton from './components/ZAlterLoginPwButton.vue';
import { uniNavBar } from '@dcloudio/uni-ui';
import fetch from '@/common/fetch.js';
import common from '@/mixin/common.js';
export default {
	components: {
		ZAlterLoginPwButton,
		uniNavBar
	},
	mixins: [common],
	data() {
		return {
			form: {
				old_login_pwd: '',
				login_pwd: '',
				c_login_pwd: ''
			},
			info: {
				login_pwd: 'New password cannot be empty',
				c_login_pwd: 'Please set a new password',
				old_login_pwd: 'origin password cannot be empty'
			},
			current: {
				title: ''
			}
		};
	},
	methods: {
		changString(obj) {
			var re = /^[0-9]+.?[0-9]*/;
			if (!re.test(obj)) {
				console.log(obj);
				return obj;
			}
			return obj.toString();
		},
		handleAlterLoginPw() {
			const form = {
				login_oldPwd: this.changString(this.form.old_login_pwd),
				login_newPwd: this.changString(this.form.login_pwd)
			};
			if (this.vaild(this.form, this.info)) {
				if (this.form.login_pwd !== this.form.c_login_pwd) {
					uni.showToast({
						title: 'The new password entered twice is inconsistent',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				fetch.request('/index/ucenter/doUpdateNewPassword', form, 'POST').then(res => {
					console.log('外面');
					console.log(res);
					if (res.code === 0 || res.code === '0') {
						uni.showToast({
							title: 'Successfully modified',
							icon: 'none',
							duration: 2000
						});
						setTimeout(() => {
							uni.navigateBack();
						}, 2100);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			}
		}
	}
};
</script>

<style lang="less" scoped>
.conwrap.no-bg .cu-form-group {
	background: none;
	padding: 0;
}
.cu-form-group {
	border-bottom: 1px solid #ccc;
	border-top: none;
	padding-left: 0;
	padding-right: 0;
}
.form {
	.cu-form-group {
		input {
			color: #555;
		}
	}
	/deep/ .bg-blue-trans {
		background-color: rgb(245, 61, 70);
		color: #fff;
	}
}
</style>
